4  Advanced Plotting: Geographical, Longitudinal, and Time Series

While we have covered the most integral and widely used plots, there are still many more to explore depending on the type of data you have.

In this chapter, we will briefly go through examples of some advanced plotting and visualisation you can do in R.

5 Geographical Data

There are many nice packages out there to represent geographical data. One of the most popular packages is leaflet, which is an open-source JavaScript library for interactive maps.

library(leaflet)
library(tidyverse)
m = leaflet() %>% addTiles()
m  
m = m %>% setView(151.64261320531202, -30.48717288612027, zoom = 17)

m %>% addPopups(
  151.64261320531202,
  -30.48717288612027,
  'Here is the <b>Mathematics, Statistics and Computer Science Building</b>, UNE'
)

You can customise the map even further:

circles <- data.frame(
  lng = c(151.643),
  lat = c(-30.487)
)

leaflet() %>%
  addTiles() %>%
  setView(lng = 151.643, lat = -30.487, zoom = 14) %>%
  addCircleMarkers(data = circles, color = "red") %>%
  addCircles(data = circles, radius = 1500)

You can read more about other customisations here. https://r-charts.com/spatial/interactive-maps-leaflet/

Want to learn more about creating maps in R? check out these resources:

names(midwest)
 [1] "PID"                  "county"               "state"               
 [4] "area"                 "poptotal"             "popdensity"          
 [7] "popwhite"             "popblack"             "popamerindian"       
[10] "popasian"             "popother"             "percwhite"           
[13] "percblack"            "percamerindan"        "percasian"           
[16] "percother"            "popadults"            "perchsd"             
[19] "percollege"           "percprof"             "poppovertyknown"     
[22] "percpovertyknown"     "percbelowpoverty"     "percchildbelowpovert"
[25] "percadultpoverty"     "percelderlypoverty"   "inmetro"             
[28] "category"            
library(dplyr)
library(sf)
library(tigris)
library(ggplot2)

options(tigris_use_cache = TRUE)

mw <- midwest

cty <- tigris::counties(cb = TRUE, year = 2022, class = "sf") %>%
  mutate(
    state = STUSPS,
    county = tolower(NAME)
  )
mw_sf <- cty %>%
  left_join(
    mw %>% mutate(county = tolower(county)),
    by = c("state", "county")
  )

mw_states <- sort(unique(mw$state))
mw_sf <- mw_sf %>% filter(state %in% mw_states)

pal <- hcl.colors(6, "Spectral", rev = TRUE, alpha = 0.75)

mw_sf <- mw_sf %>%
  filter(!is.na(percbelowpoverty)) %>%
  mutate(
    cls = cut(percbelowpoverty,
              breaks = c(0, 5, 10, 15, 20, 25, Inf),
              include.lowest = TRUE),
    cls = factor(cls, labels = c("(0-5]", "(5-10]", "(10-15]", "(15-20]", "(20-25]", "(25+]"))
  )

ggplot(mw_sf) +
  geom_sf(aes(fill = cls), color = "white", linewidth = 0.1) +
  scale_fill_manual(values = pal, drop = FALSE, name = "% below poverty") +
  theme_void() +
  labs(title = "Midwest counties", subtitle = "Choropleth by poverty rate")

6 Longitudinal

7 Times Series

USAccDeaths
       Jan   Feb   Mar   Apr   May   Jun   Jul   Aug   Sep   Oct   Nov   Dec
1973  9007  8106  8928  9137 10017 10826 11317 10744  9713  9938  9161  8927
1974  7750  6981  8038  8422  8714  9512 10120  9823  8743  9129  8710  8680
1975  8162  7306  8124  7870  9387  9556 10093  9620  8285  8466  8160  8034
1976  7717  7461  7767  7925  8623  8945 10078  9179  8037  8488  7874  8647
1977  7792  6957  7726  8106  8890  9299 10625  9302  8314  8850  8265  8796
1978  7836  6892  7791  8192  9115  9434 10484  9827  9110  9070  8633  9240
library(dygraphs)
library(xts)

# built-in monthly ts object
x <- USAccDeaths

# convert ts -> xts with a monthly Date index
dates <- seq.Date(from = as.Date("1973-01-01"),
                  by   = "month",
                  length.out = length(x))

x_xts <- xts(as.numeric(x), order.by = dates)
colnames(x_xts) <- "US Accident Deaths"

# plot
dygraph(x_xts) %>%
  dyRangeSelector()

We can also have a static plot:

library(ggplot2)
library(dplyr)

data <- data.frame(
  date  = as.Date(airmiles),
  value = as.numeric(airmiles))

data %>%
  ggplot(aes(x = date, y = value)) +
  geom_area(fill = "#69b3a2", alpha = 0.5) +
  geom_line(color = "#69b3a2") +
  labs(x="year", y="Airline passenger miles (millions)")+
  scale_x_date(expand=c(0.01,0))+
  scale_y_continuous(breaks = seq(0, 30000, by = 5000))+
  theme_minimal() 

# Load dataset (built-in)
data <- data.frame(
  date  = as.Date(economics$date),
  value = economics$unemploy)

data %>%
  ggplot(aes(x = date, y = value)) +
  geom_area(fill = "lightblue", alpha = 0.5) +
  geom_line(color = "darkblue") +
  labs(x="year", y="Number of US Unemployed (in thousands)")+
  scale_y_continuous(breaks = seq(0, 20000, by = 2000))+
  theme_minimal()